﻿using MyDatabase;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DataBaseDAO
{
    public class UserDAO
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User CheckLogin(string username, string password)
        {
            //实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            //根据账号，密码进行查询
            var data = db.Users.Where(p => p.Username == username && p.Password == password).FirstOrDefault();
            return data;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public List<User> GetUsersList(string keyWord)
        {
            //实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();

            var data = db.Users.ToList();
            if (!string.IsNullOrEmpty(keyWord))
            {
                data = data.Where(p => p.Realname.Contains(keyWord)).ToList();
            }
            return data;
        }

        /// <summary>
        /// 删除用户数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool DelUser(int ID)
        {
            //实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            //先找出一条数据
            var data = db.Users.Where(p => p.Id == ID).FirstOrDefault();
            if (data != null)
            {
                //再将这条数据删除
                db.Users.Remove(data);
                db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取单条用户数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public User GetSingleUser(int ID)
        {
            //实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            //先找出一条数据
            var data = db.Users.Where(p => p.Id == ID).FirstOrDefault();
            return data;
        }

        /// <summary>
        /// 修改单个用户数据
        /// </summary>
        /// <param name="user"></param>
        public void UpdateSingleUser(User user)
        {
            //实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            //先找出一条数据
            var data = db.Users.Where(p => p.Id == user.Id).FirstOrDefault();
            //再修改这条数据
            data.Realname = user.Realname;
            data.Password = user.Password;
            data.Power = user.Power;
            data.Updatetime = DateTime.Now;
            db.SaveChanges();
        }

        /// <summary>
        /// 添加一条用户数据
        /// </summary>
        /// <param name="user"></param>
        public void InsertSingleUser(User user)
        {
            //实例化数据库上下文类
            MyMVCContext db = new MyMVCContext();
            user.Createtime = DateTime.Now;
            user.Updatetime = DateTime.Now;
            db.Users.Add(user);
            db.SaveChanges();
        }
    }
}